# coding: utf-8

from collections import namedtuple

from flask import (
    render_template, redirect, url_for, flash, request, current_app, jsonify,
)
from flask_login import login_required

from common.demand_util import get_expected_bid_point
from common.constant import WATERING_INDEX, SECOND_WATERING_INDEX, OTHER_WATERING_INDEX
from . import water_bp
from .forms import WaterFactorFrom, WaterPreviewForm
from app.op.models import Area
from app.op.constant import AreaType


@water_bp.route('/water_factor_list', methods=['GET'])
@login_required
def water_factor_list():
    """
    add by zhouhl 2018-04-25
    所有城市的浇水系数
    """
    areas = Area.query.filter(
        Area.type == AreaType.CITY.value
    ).order_by(
        Area.id.asc()
    )

    return render_template(
        'water_factor_list.html',
        areas=areas,
    )


@water_bp.route('/water_factor/<int:area_id>', methods=['GET', 'POST'])
@login_required
def water_factor(area_id):
    """
    add by zhouhl 2018-04-25
    调整某个城市的浇水系数
    """
    form = WaterFactorFrom()

    area = Area.get_by_id(area_id)
    if form.validate_on_submit():
        area.water_factor = form.water_factor.data
        flash('修改成功')
        return redirect(url_for('water_bp.water_factor_list'))

    form = WaterFactorFrom(obj=area)
    return render_template(
        'water_factor_area.html',
        form=form,
    )


@water_bp.route('/water_preview', methods=['GET', 'POST'])
@login_required
def water_preview():
    """
    created by chenhj at 2018/4/26

    """
    watering_index = current_app.redis.get(WATERING_INDEX) or '1.0'
    sec_watering_index = current_app.redis.get(SECOND_WATERING_INDEX) or '1.0'
    other_watering_index = current_app.redis.get(OTHER_WATERING_INDEX) or '1.0'
    if request.method == 'GET':
        form = WaterPreviewForm()
        return render_template(
            'water_preview.html', form=form,
            watering_index=watering_index,
            sec_watering_index=sec_watering_index,
            other_watering_index=other_watering_index
        )
    data = request.get_json()
    fake_Demand = namedtuple('Demand', 'id cate type size color span published_count')
    fake_Area = namedtuple('Area', 'water_factor')
    fake_demand1 = fake_Demand(
        id=0,
        cate=int(data['cate']),
        type=int(data['type']),
        size=float(data['size']),
        color=int(data['color']),
        span=data['span'],
        published_count=1
    )
    fake_demand2 = fake_Demand(
        id=0,
        cate=int(data['cate']),
        type=int(data['type']),
        size=float(data['size']),
        color=int(data['color']),
        span=data['span'],
        published_count=2
    )
    fake_demand3 = fake_Demand(
        id=0,
        cate=int(data['cate']),
        type=int(data['type']),
        size=float(data['size']),
        color=int(data['color']),
        span=data['span'],
        published_count=3
    )
    fake_area = fake_Area(water_factor=data['city_factor'])
    area = Area.get_by_args(name=data['city'], type=AreaType.CITY.value)
    first_old_value = get_expected_bid_point(fake_demand1, area)
    second_old_value = get_expected_bid_point(fake_demand2, area)
    other_old_value = get_expected_bid_point(fake_demand3, area)
    first_new_value = int(
        get_expected_bid_point(fake_demand1, fake_area)
        / float(watering_index)
        * float(data['watering_index'])
    )
    second_new_value = int(
        get_expected_bid_point(fake_demand2, fake_area)
        / float(sec_watering_index)
        * float(data['sec_watering_index'])
    )
    other_new_value = int(
        get_expected_bid_point(fake_demand3, fake_area)
        / float(other_watering_index)
        * float(data['other_watering_index'])
    )

    return jsonify(
        first_old_value=min(first_old_value, 200),
        first_new_value=min(first_new_value, 200),
        second_new_value=min(second_new_value, 200),
        second_old_value=min(second_old_value, 200),
        other_new_value=min(other_new_value, 200),
        other_old_value=min(other_old_value, 200)
    )


@water_bp.route('/water_preview/factor')
@login_required
def get_water_preview_factor():
    """
    created by chenhj at 2018/4/26

    """
    args = request.args
    city = args.get('city')
    if city and city != "-1":
        c = Area.get_by_args(
            name=city, type=AreaType.CITY.value
        )
        return jsonify(city_factor=float(c.water_factor))
    return jsonify(city_factor=0)
